package com.woniu.sharepark.leasesystem.lessor.service.impl;


import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.constant.SystemConstant;
import com.woniu.sharepark.leasesystem.lessor.dao.CarParkDAO;
import com.woniu.sharepark.leasesystem.lessor.dto.CarParkDTO;
import com.woniu.sharepark.leasesystem.lessor.pojo.CarPark;
import com.woniu.sharepark.leasesystem.lessor.service.CarParkService;
import com.woniu.sharepark.leasesystem.lessor.util.CarParkUtil;
import com.woniu.sharepark.util.UploadImageUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
 * 该类为CarParkService接口的实现类
 * 方法的详情介绍烦请查看接口注释
 *
 * @Author Zongjie.Li
 * @Date 2019/6/4
 */
@Service
public class CarParkServiceImpl implements CarParkService {

    @Resource
    private CarParkDAO carParkDAO;

    @Autowired
    private CarPark carPark;

    @Override
    public CarPark findCarPark(Integer carparkId) {
        return carParkDAO.selectByPrimaryKey(carparkId);
    }

    @Override
    public List<CarPark> findAllCarParkPage(Integer currentPage, Integer pageSize, CarPark carPark) {
        return carParkDAO.selectAllCarParkPage(currentPage, pageSize, carPark);
    }

    @Override
    public JsonResult addCarPark(CarParkDTO carParkDTO) {
        JsonResult jsonResult = CarParkUtil.dtoVerify(carParkDTO);
        if (jsonResult != null) {
            return jsonResult;
        }
        String fileName;
        String picName;
        try {
            fileName = UploadImageUtil.uploadImage(carParkDTO.getPropertyInfo());
            picName = UploadImageUtil.uploadImage(carParkDTO.getCarParkPic());
        } catch (IOException e) {
            e.printStackTrace();
            return JsonResult.error("图片上传出错");
        }
        Set<Integer> setResult = CarParkUtil.carParkRepeatVerify(carParkDTO.getNumberStart(), carParkDTO.getNumberEnd(), carParkDAO.selectCarParkNumberByCommunityID(carParkDTO.getCommunityID()));
        if (setResult != null && setResult.size() > 0) {
            return new JsonResult("添加失败，您的车位已经添加过了", 500, setResult);
        }
        Long timeStart = System.currentTimeMillis();
        carParkDAO.insertByCarParkDTO(CarParkUtil.toListValues(carParkDTO, ((Login) SecurityUtils.getSubject().getSession().getAttribute("currentUser")).getUserInfo().getuId(), fileName,picName));
        System.out.println("Time Spend : " + (System.currentTimeMillis() - timeStart));
        return JsonResult.ok("恭喜您，上传车位成功");
    }

    @Override
    public List<CarPark> findCarParkSelective(CarPark carPark) {
        return carParkDAO.selectCarParkSelective(carPark);
    }

    /**
     * 物业方审核车位信息
     * @param ids
     * @return
     */
    @Override
    public int checkCarparks(Integer[] ids) {
        return carParkDAO.updateStatusByIds(ids);
    }

    /**
     * 平台方审核车位信息
     * @param id
     * @return
     */
    @Override
    public int updateCarpark(Integer id) {
        return carParkDAO.updateStatusById(id);
    }
    @Override
    public JsonResult findUsefulCarPark() {
        carPark = new CarPark();
        carPark.setcState("未租");
        List<CarPark> carParkList = carParkDAO.selectCarParkSelective(carPark);
        for (CarPark carParkGet : carParkList){
            CarParkUtil.informationHide(carParkGet);
        }
        return new JsonResult("Success",SystemConstant.SUCCESS_CODE,carParkList);
    }

    @Override
    public JsonResult findCarParkById(Integer id) {
        carPark = carParkDAO.selectByPrimaryKey(id);
        CarParkUtil.informationHide(carPark);
        return new JsonResult("Success",SystemConstant.SUCCESS_CODE,carPark);
    }

    @Override
    public int updateByPrimaryKeySelective(CarPark carPark) {
        return carParkDAO.updateByPrimaryKeySelective(carPark);
    }

    @Override
    @Cacheable(value = "carParkCount")
    public int findAllCount() {
        return carParkDAO.selectAllCount();
    }

    /**
     * 查找当前用户拥有的车位
     *
     * @return com.woniu.sharepark.common.JsonResult
     * @Author Zongjie.Li
     * @Date 2019/6/20
     * @Version 1.0
     */
    @Override
    public JsonResult findMyCarPark() {
        return new JsonResult("",SystemConstant.SUCCESS_CODE,carParkDAO.selectCarParkByUid(((Login) SecurityUtils.getSubject().getSession().getAttribute("currentUser")).getUserInfo().getuId()));
    }

    @Override
    public JsonResult getIncomeYesterday() {
        return new JsonResult("",SystemConstant.SUCCESS_CODE,carParkDAO.getIncomeYesterday(((Login) SecurityUtils.getSubject().getSession().getAttribute("currentUser")).getUserInfo().getuId()));
    }

    @Override
    public JsonResult upCarPark(CarPark carPark) {
        carPark.setcState("未租");
        carParkDAO.updateByPrimaryKeySelective(carPark);
        return JsonResult.ok("上架成功");
    }

    @Override
    public JsonResult downCarPark(CarPark carPark) {
        carPark.setcState("下架");
        carParkDAO.updateByPrimaryKeySelective(carPark);
        return JsonResult.ok("下架成功");
    }
}
